package com.example.springbott.service;

import com.example.springbott.bean.MarketCoupon;
import com.example.springbott.bean.MarketCouponUser;
import com.example.springbott.bean.MarketCouponUserExample;
import com.example.springbott.common.CommonData;
import com.example.springbott.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/20 20:47
 */
@Service
public class MarketCouponUserServiceIml implements MarketCouponUserService {
    @Resource
    MarketCouponUserMapper marketCouponUserMapper;

    /**
     * 用于展示优惠券详情
     * @param page
     * @param limit
     * @param couponId
     * @param sort
     * @param order
     * @return
     */
    @Override
    public CommonData<MarketCouponUser> listUser(Integer page, Integer limit, Integer couponId, Integer userId,Short status, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        if (!("".equals(couponId)||couponId==null)){
            criteria.andCouponIdEqualTo(couponId);
        }
        if (!("".equals(userId)||userId==null)){
            criteria.andUserIdEqualTo(userId);
        }
        if (!("".equals(status)||status==null)){
            criteria.andStatusEqualTo(status);
        }

        List<MarketCouponUser> list = marketCouponUserMapper.selectByExample(couponUserExample);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<MarketCouponUser>(list);
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketCouponUser> list1 = pageInfo.getList();
        CommonData<MarketCouponUser> data = new CommonData<>(page1, pages,  (int) total,limit1, list1);
        return data;
    }
}
